package com.xungou.utils;

import com.xungou.resource.pojo.Resource;
import com.xungou.resource.service.IResourceService;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class FileUtil {
    public static Resource uploadFile(MultipartFile file, String modelName, IResourceService iResourceService) {
        String webRealRoot = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/");
        modelName = "resource"+ File.separator+ modelName;
        File path = new File(webRealRoot  + File.separator +modelName);
        if (!path.exists() && !path.isDirectory()) {
            path.mkdirs();
        }
        if (!file.isEmpty()) {
            try {
                String newFileName = getUUID() + "."
                        + getPostfix(file.getOriginalFilename());
                String filePath = path.getAbsolutePath() + File.separator + newFileName;

                file.transferTo(new File(filePath));
                Resource resource = new Resource();
                resource.setName(file.getOriginalFilename());
                resource.setPath(modelName + File.separator + newFileName);
                iResourceService.insert(resource);

                return resource;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String getPostfix(String inputFilePath) {
        String postfix = null;
        if (null != inputFilePath && !"".equals(inputFilePath.trim())) {
            int idx = inputFilePath.lastIndexOf(".");
            if (idx > 0) {
                postfix = inputFilePath.substring(idx + 1, inputFilePath.trim().length());
            }
        }
        return postfix;
    }

    public static ResponseEntity<byte[]> download(Resource resource) throws IOException {
        String webRealRoot = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/");
        String path=webRealRoot  + File.separator +resource.getPath();
        File file=new File(path);
        HttpHeaders headers = new HttpHeaders();
        String fileName=new String(resource.getName().getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
                headers, HttpStatus.CREATED);
    }

    public static String getUUID(){
        String s = UUID.randomUUID().toString();
        //去掉“-”符号
        return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);
    }
}
